import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import store from '../store/index'
// import store from '../vuex/store.js'
import {
    getToken
} from '../utils/auth';

axios.defaults.withCredentials = true
Vue.prototype.$ajax = axios;
let vm = new Vue();

const baseUrl = process.env.BASE_API


// request拦截器
axios.interceptors.request.use((config) => {
    if (store.getters.token) {
    config.headers['Authorization'] = getToken(); // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
    }
    let type = config.headers['Type'];
    if (undefined !== type && type == "json") {
    config.data = config.data
    return config;
    }
    if (config.method === 'post') {
    config.data = qs.stringify(config.data);
    }
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
    return config;
   }, (error) => {
    return Promise.reject(error);
   });

//返回状态判断
axios.interceptors.response.use((res) => {
    if (res.data.code == 224) {
        let $config = {
        top: 50,
        duration: 1,
        content: '登陆超时',
        onClose: function() {
            window.location.href = '/#/login';
        }
        }
    } else {
      return res;
    }
}, (error) => {
    if (!error.response) {
    return Promise.reject(error);
    }
    if (401 == error.response.status) {
        let $config = {
        top: 50,
        duration: 1,
        content: '登录失效 ， 请重新登录',
        onClose: function() {
            window.location.href = '/#/login';
        }
        }
        vm.$Message.warning($config);
    } else {
        return Promise.reject(error);
    }
});

export function fetch(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
        .then(response => {
            resolve(response);
        }, err => {
            reject(err);
        })
        .catch((error) => {
            reject(error)
        })
    })
}
export function doPost(url, params) {
    // let url = baseUrl + url
    let config = {
        headers: {
            'Type': 'json' //
        }
    };
    return new Promise((resolve, reject) => {
        axios.post(url, params, config)
        .then(response => {
            resolve(response)
        }, err => {
            reject(err)
        })
        .catch((error) => {
            reject(error)
        })
    })
}

export function get(url) {
    // let url = baseUrl + url
    return new Promise((resolve, reject) => {
        axios.get(url)
        .then(response => {
            resolve(response)
        }, err => {
            reject(err)
        })
        .catch((error) => {
            reject(error)
        })
    })
}
export default {
    
    Gettodylist(params) {//今日销售额
        // return get(baseUrl+'/managerapi/summary/todaydata', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/summary/todaydata#!method=get', params);
    },
   
    Weekdata(params) { // 机器销售额
        // return get(baseUrl+'/managerapi/vendorsummary/weekdata', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendorsummary/weekdata#!method=get', params);
    },
    
    mounthlist(params) {//月销售额
        // return get(baseUrl+'/managerapi/summary/monthdata', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/summary/monthdata#!method=get', params);
    },
   
    weekSaledata(params) { //本周销售额
        // return get(baseUrl+'/managerapi/summary/weekSaledata', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/summary/weekSaledata#!method=get', params);
    },
    
    LineChart(params){//销售总额的折线图
        // return get(baseUrl+`/managerapi/hoursales/hourdata/${params}`,params);
        return get(`https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/hoursales/hourdata/${params}#!method=get`, params);
    },
  
    PayMore(params){//销售支付比图图
        // return doPost(baseUrl+`/managerapi/summary/payTypeProportion`,params);
        return doPost('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/summary/payTypeProportion#!method=get', params);
    },
    
    Shoppinglist(params) {//商品排行榜
        // return get(baseUrl+'/managerapi/rank/productSale', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/rank/productSale#!method=get', params);
    },
   
   Vendingmachine(params) { //机器排行榜
        // return get(baseUrl+'/managerapi/rank/vendorSale', params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/rank/vendorSale#!method=get', params);
    },
    
    PointCharge(params) {//组位
        // return get(baseUrl+`/managerapi/rank/groupSale`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/rank/groupSale#!method=get', params);
    },
    
    GetYearList(params) {//一年的销售汇总
        // return get(baseUrl+`/managerapi/summary/yearMonthSaledata`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/summary/yearMonthSaledata#!method=get', params);
    },
    
    defeated (params){//售货机出货失败数据
        // return get(baseUrl+`/managerapi/vendor/summary/deliverFailData`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/deliverFailData#!method=get', params);
    },
   
    Rateroad (params){// 售货机空货率数据
    // return get(baseUrl+`/managerapi/vendor/summary/emptyProductData`, params);
    return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/emptyProductData#!method=get', params);
   },
    
    emptyroad (params){// 售货机空道率数据
        // return get(baseUrl+`/managerapi/vendor/summary/emptyRoadData`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/emptyRoadData#!method=get', params);
    },
   
    jurisdiction (params){ //  用户管辖组数据
        // return get(baseUrl+`/managerapi/vendor/summary/listGroupData`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/listGroupData#!method=get', params);
    },
   
    failure(params){ // 商品报损率数据
        // return get(baseUrl+`/managerapi /vendor/summary/productScrapData/${params}`, params);
        return get(`https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/productScrapData#!method=get`, params);
    },
   
    accidental(params){ // 售货机故障数据
        // return get(baseUrl+`/managerapi/vendor/summary/troubleData/${params}`, params);
        return get(`https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/troubleData#!method=get`, params);
    },
  
    online(params){  //售货机在线情况数据
        // return get(baseUrl+`/managerapi/vendor/summary/vendorOnlineData`, params);
        return get('https://easy-mock.com/mock/5c233cf097678c2abdd286b6/managerapi/managerapi/vendor/summary/vendorOnlineData#!method=get', params);
    },
    historySaledata(params){  //历史数据：周、月销售数据
        return get(baseUrl+`/managerapi/report/analyse/historySaledata`, params);
       
    },
}